//
//  DyAFnetWorking.m
//  AFN封装demol
//
//  Created by dllo on 15/11/25.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "DyAFnetWorking.h"
#import <CommonCrypto/CommonDigest.h>

@implementation DyAFnetWorking

+ (void)POSTAfnNetworkingWithUrl:(NSString *)url WithParameters:(NSDictionary *)para Block:(void (^)(id result))block{
    
    AFHTTPSessionManager *netManager = [AFHTTPSessionManager manager];
    netManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain",@"application/x-javascript",nil];
   [netManager POST:url parameters:para success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       block(responseObject);
   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您当前网络不佳" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
       [alterView show];
       NSLog(@"失败==== %@",error);
   }];
}
+ (void)GETJRAfnNetworkingWithUrl:(NSString *)url Block:(void (^)(id result))block {
    AFHTTPSessionManager *mannager = [AFHTTPSessionManager manager];
    [mannager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain",@"application/x-javascript",nil]];
    [mannager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        block(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您当前网络不佳" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alterView show];
        NSLog(@"失败==== %@",error);
    }];
}



+ (NSDictionary *)convertStrToBodyDic: (NSString *)bodyStr {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSArray *paraArray = [bodyStr componentsSeparatedByString:@"&"];
    for (NSString *tempStr in paraArray) {
        NSArray *keyValueArray = [tempStr componentsSeparatedByString:@"="];
        [dic setObject:keyValueArray[1] forKey:keyValueArray[0]];
    }
    NSDictionary *bodyDic = [dic copy];
    return bodyDic;
}


+ (NSString*) sha1:(NSString*)input {
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.length];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    
    CC_SHA1(data.bytes, (unsigned int)data.length, digest);
    
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return output;
}

+ (void)cachePostWithURL:(NSString *)URLStr WithbodyStr:(NSString *)bodyStr success:(void (^)(id result))successBlock failure:(void (^)(id nonet))failureBlock {
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
    NSString *fileName;
    fileName = [self sha1:[NSString stringWithFormat:@"%@_%@", URLStr, bodyStr]];
    NSString *cacheFilePath = [cachePath stringByAppendingPathComponent:fileName];
    NSDictionary *para = [self convertStrToBodyDic:bodyStr];
    AFHTTPSessionManager *netManager = [AFHTTPSessionManager manager];
    netManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain",@"application/x-javascript",nil];
    
    [netManager POST:URLStr parameters:para success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         NSDictionary *dic = responseObject;
       [dic writeToFile:cacheFilePath atomically:YES];
        successBlock(responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:cacheFilePath];
        if (dic) {
            successBlock(dic);
        } else {
            failureBlock(error);
        }
            UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您当前网络不佳" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alterView show];
            NSLog(@"失败==== %@",error);
        
    }];

}


@end
